from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
from src.config import settings
from src.schemas import DeptUpdate
from src.schemas.role_schema import RoleInfo


class UserInfo(BaseModel):
    model_config = ConfigDict(
        from_attributes=True,
        arbitrary_types_allowed=True,
    )
    id: int = Field(description="主键ID")
    username: str = Field(..., max_length=20, description="用户名")
    nickname: str | None = Field(..., max_length=20, description="昵称")
    avatar: str | None = Field(None, max_length=255, description="头像")
    email: str | None = Field(None, max_length=64, description="邮箱")
    sex: str | None = Field(default="2", description="用户性别（0男 1女 2未知）")
    phone: str | None = Field(None, max_length=11, description="电话")
    is_super: Literal["0", "1"] = Field(default="0", description="（0普通 1管理员）")
    status: Literal["1", "2"] = Field(default="1", description="是否启用1:启用，2:禁用")
    login_ip: str | None = Field(None, max_length=30, description="ip地址")
    remark: str | None = Field(None, max_length=255, description="描述")
    dept_id: int | None = Field(None, description="部门ID")
    # 岗位id集合
    post_ids: list[int] | None = Field(default=None, description="岗位id集合")
    # 角色id集合
    role_ids: list[int] | None = Field(default=None, description="角色id集合")
    roles: list[RoleInfo] | None = Field(default=None, description="角色集合")
    data_range: int = Field(default=0, description="数据范围")
    dept_ids: list[int] | None = Field(default=None, description="部门id集合")
    dept: DeptUpdate | None = Field(default=None, description="部门对象")
    permissions: set[str] | None = Field(default=None, description="权限集合")
    password: str = Field(..., description="密码")

    def is_super_admin(self) -> bool:
        # 判断是否是角色key是否ROLE_ADMIN，先检查roles属性是否存在
        if not hasattr(self, "roles") or self.roles is None:
            return False
        role_list = [r.role_key for r in self.roles]
        return settings.auth.SUPER_ADMIN in role_list
